package com.google.code.synchronized_player;

import java.io.UnsupportedEncodingException;

public class Command {
	public static final String FIELD_SEPARATOR=" ";
	public String getName() { return _repr[0]; }
	public String getArg(int idx) { return _repr[idx+1]; }
	
	private String[] _repr;
	
	public static Command Parse(byte[] buffer, int size)
	{
		try {
			String repr = new String(buffer,0,size,"US-ASCII");
			return Command.Parse(repr);
			} 
		catch (UnsupportedEncodingException e) 
		{ 
			return null;
		}
		
	}

	public static Command Parse(String repr) {
		 Command c = new Command();
		 c._repr = repr.split(" ");
		 return c;
	}
	
	public byte[] getBytes() 
	{
		try
		{
			StringBuilder sb = new StringBuilder();
		
			for (String s : _repr)
			{
				sb.append(s);
				sb.append(FIELD_SEPARATOR);
			}
			
			return sb.toString().getBytes("US-ASCII");
		}
		catch (Exception e)
		{
			return null;
		}
		 
	}

}
